<template>
    <div v-if="row.actions.length>2">
        <a @click="handleClickMore(row.actions[0].name, row)">{{ row.actions ? row.actions[0].label : '' }}</a>
        <Divider type="vertical" />
        <Dropdown @on-click="(name) => handleClickMore(name, row)"
                  transfer>
            <a>更多
                <Icon type="ios-arrow-down" /></a>
            <DropdownMenu slot="list">
                <DropdownItem v-for="(item, i) in row.actions.slice(1)"
                              :key="i"
                              :name="item.name">{{ item.label }}</DropdownItem>
            </DropdownMenu>
        </Dropdown>
    </div>
    <div v-else>
        <a v-if="row.actions && showBtn(row.actions[0].label)"
           @click="handleClickMore(row.actions[0].name, row)">{{ row.actions[0].label }}</a>
        <Divider v-if="row.actions && row.actions.length>1"
                 type="vertical" />
        <a v-if="row.actions && row.actions.length>1 && showBtn(row.actions[1].label)"
           @click="handleClickMore(row.actions[1].name, row)">{{ row.actions[1].label }}</a>
    </div>
</template>
<script>
    import { ApiRequest } from '@api/basePage';
    import dynamictableactions from '@/mixins/dynamictableactions'; // 混入通用代码
    import qwInput from '@/components/common/formItem/qwInput'; // input组件
    import qwSelect from '@/components/common/formItem/qwSelect'; // select组件
    import { mapGetters } from 'vuex';
    import { getBtnsId } from '@/libs/btnsPermission'
    export default {
        mixins: [
            dynamictableactions
        ],
        components: { qwInput, qwSelect },
      computed: {
        ...mapGetters('permissions/systemBtns', [
          'showBtn'
        ]),
      },
        data () {
            return {
                auditModal: false,
                auditCompsData: [ // 组件信息
                    {
                        fileName: 'qwSelect', // 组件文件名称
                        fileUrl: 'common', // 组件目录关键字 common：通用模板 custom：自定义模板
                        data: {
                            name: 'chooseReason',
                            value: '', // 单选时只接受 String 或 Number，多选时只接受 Array
                            label: '选择原因',
                            type: 'select',
                            placeholder: '请选择权益卡',
                            filterable: true, // 是否可搜索
                            multiple: false, // 是否多选
                            selectList: [
                                {
                                    value: '0',
                                    label: '商品重复'
                                },
                                {
                                    value: '1',
                                    label: '商品不符合规范'
                                },
                                {
                                    value: '2',
                                    label: '描述信息不相符'
                                }
                            ]
                        }
                    },
                    {
                        fileName: 'qwInput', // 组件文件名称
                        fileUrl: 'common', // 组件目录关键字 common：通用模板 custom：自定义模板
                        data: {
                            name: 'inputReason', // 字段name
                            value: '', // 字段value值 String 或 Number
                            label: '输入原因', // 字段label
                            type: 'textarea', // 字段类型
                            inputType: 'textarea', // input 类型
                            placeholder: '请输入内容', // 字段placeholder
                            autosize: {
                                minRows: 3, // 最少行数
                                maxRows: 5 // 最大行数
                            }
                        }
                    }
                ],
                auditFormData: {
                    chooseReason: '',
                    inputReason: ''
                },
                aloading: false,
                cloading: false
            }
        },
        created () {
        },
        methods: {
            //  操作更多选择
            handleClickMore (name, row) {
                let obj = {}; // 动作的对象信息
                row.actions.forEach(item => {
                    if (name === item.name) {
                        obj = item;
                        return false;
                    }
                })
                this.setTableRowData(row)
                if (obj.handleType === 'link') { // 操作类型为链接时，拼接参数跳转
                    let curBtnId = getBtnsId('编辑')
                    sessionStorage.setItem('buttonId', curBtnId)
                    let jumpPath = this.jumpPath + obj.name;
                    let queryObj = {};
                    if (obj.params && obj.params.length > 0) {
                        obj.params.forEach(item => {
                            queryObj[item] = row[item]
                        });
                    } else {
                        queryObj = { 'uuid': row.uuid }
                    }
                    this.$router.push({ path: jumpPath, query: queryObj })
                } else if (obj.handleType === 'confirm') { // 操作类型为confirm时 打开modal弹窗
                    this.auditModal = true;
                } else if (obj.handleType === 'alert') {
                    if (name === 'cancel') {
                        let uuid = this.$route.query.uuid
                        this.$Modal.confirm({
                            title: '取消关联',
                            content: '确定要取消关联吗？',
                            onOk: () => {
                                let data = {
                                    needDeleteUuids: row.uuid,
                                    templateUuid: uuid
                                }
                                // 开始请求商品列表
                                ApiRequest(this.requestApi.getCancel.url, this.requestApi.getCancel.type, data, true).then(res => {
                                    this.$Message.success('取消成功');
                                    this.actionsChange();
                                })
                            }
                        });
                    }
                    if (name === 'delete') {
                        this.$Modal.confirm({
                            title: '删除',
                            content: '确定要删除该选项吗？',
                            onOk: () => {
                                let curBtnId = getBtnsId('删除')
				                sessionStorage.setItem('buttonId', curBtnId)
                                let data = {
                                    uuid: row.uuid
                                }
                                // 开始请求商品列表
                                ApiRequest(this.requestApi.toDelete.url, this.requestApi.toDelete.type, data, true).then(res => {
                                    if(res.retStatus != '1') return;
                                    this.$Message.success('该选项已成功删除');
                                    this.actionsChange();
                                })
                            }
                        });
                    }
                    if (name === 'LowerShelf') {
                        this.$Modal.confirm({
                            title: '下架商品',
                            content: '确定要下架该商品吗？',
                            onOk: () => {
                                let data = {
                                    uuidList: row.uuid
                                }
                                // 开始请求商品列表
                                ApiRequest(this.requestApi.toLowerShelf.url, this.requestApi.toLowerShelf.type, data, true).then(res => {
                                    this.$Message.success('该商品已成功下架');
                                    this.actionsChange();
                                })
                            }
                        });
                    }
                }
            },
            // 组件内数据改变
            handleChangeFormData (data) {
                this.$set(this.$data, 'auditFormData', data);
            },
            // 审核通过
            handleAgree (name) {
                if (this.auditFormData.chooseReason === '' && this.auditFormData.inputReason === '') {
                    this.$Message.error('请选择原因!');
                    return false;
                }
                this.aloading = true;
                let data = {
                    auditType: '0', // 审核通过
                    uuid: this.row.uuid,
                    chooseReason: this.auditFormData.chooseReason,
                    inputReason: this.auditFormData.inputReason
                }
                // 开始请求商品列表
                ApiRequest(this.requestApi.toAudit.url, this.requestApi.toAudit.type, data).then(res => {
                    this.aloading = false;
                    this.$Message.success('审核成功');
                    this.auditModal = false;
                    this.actionsChange();
                })
            },
            // 拒绝审核
            handleCancel (name) {
                if (this.auditFormData.inputReason === '') {
                    this.$Message.error('请输入拒绝原因!');
                    return false;
                }
                this.aloading = true;
                let data = {
                    auditType: '1', // 审核不通过
                    uuid: this.row.uuid,
                    chooseReason: this.auditFormData.chooseReason,
                    inputReason: this.auditFormData.inputReason
                }
                // 开始请求商品列表
                ApiRequest(this.requestApi.toAudit.url, this.requestApi.toAudit.type, data).then(res => {
                    this.aloading = false;
                    this.$Message.success('拒绝成功');
                    this.auditModal = false;
                    this.actionsChange();
                })
            }
        }
    }
</script>
